
package me.zhengjie.modules.website.repository;

import me.zhengjie.modules.website.domain.AccessLogs;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author system
 * @since 2024-12-02 16:18:43
 */
@Component
public class AccessLogsSpecifications {

    public static Specification<AccessLogs> queryList(AccessLogs accessLogs) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (accessLogs.getId() != null) {
                predicates.add(builder.equal(root.get("id" ).as(Long.class), accessLogs.getId()));
            }
            if (accessLogs.getIpAddress() != null) {
                predicates.add(builder.like(root.get("ipAddress" ).as(String.class), "%" + accessLogs.getIpAddress() + "%" ));
            }
            if (accessLogs.getVisitTime() != null) {
                predicates.add(builder.equal(root.get("visitTime" ).as(Date.class), accessLogs.getVisitTime()));
            }
            if (accessLogs.getPage() != null) {
                predicates.add(builder.like(root.get("page" ).as(String.class), "%" + accessLogs.getPage() + "%" ));
            }
            Predicate[] p = new Predicate[predicates.size()];
            return query.where(builder.and(predicates.toArray(p))).getRestriction();
        };
    }

}
